#include "note.h"
#include "object.h"
#include "Vector3.h"
#include <iostream>

double Note::SCALE = 0.0;
double Note::ERRORTOL = 0.0;
double Note::TOTALLENGTH = 0.0;
int Note::SOUNDOFFSET = 0;
GLuint Note::texId_missed = 0;
GLuint Note::texId_ok = 0;
GLuint Note::texId_doublestrum = 0;
GLuint Note::texId_missed_band = 0;
GLuint Note::texId_standard_bands[5] = {0,0,0,0,0};
GLuint Note::texId_highlights[5] = {0,0,0,0,0};
GLuint Note::texId_standards[5] = {0,0,0,0,0};
GLuint Note::texId_nostrums[5] = {0,0,0,0,0};
Shader* Note::note_shader = NULL;
int Note::bande_geom = 0;
int Note::note_geom = 0;

Note::Note()
{
	m_time = 0.0;
	m_duration = 0.0;
	m_track = 0;
	m_tostrum = true;
	m_strummed = false;
	m_hammered = false;
	m_noteObj = NULL;
	m_bandeObj = NULL;
	m_chord = false;
	m_missed = false;
	m_interrupted = false;
}

Note::Note(double time,double duration,int track)
{
	m_time = time;
	m_duration = duration;
	m_track = track;
	m_tostrum = true;
	m_strummed = false;
	m_hammered = false;
	m_chord = false;
	m_noteObj = NULL;
	m_bandeObj = NULL;
	m_missed = false;
	m_interrupted = false;
}

void Note::resetObjs()
{
	m_noteObj = m_bandeObj = NULL;
	m_strummed = false;
	m_hammered = false;
}

void Note::setMissed(bool b)
{
	m_missed=b;
	m_noteObj->setTexture(Note::texId_missed);
	if(m_bandeObj) m_bandeObj->setTexture(Note::texId_missed_band);
}

void Note::setInterrupted(bool b)
{
	m_interrupted=b;
	if(m_bandeObj) m_bandeObj->setTexture(Note::texId_missed_band);
}

void Note::setStrummed(bool b)
{
	m_strummed=b;
	m_noteObj->setTexture(Note::texId_ok);
	m_noteObj->setRender(false);
	if(m_bandeObj) m_bandeObj->setTexture( Note::texId_highlights[m_track] );
}

void Note::tagDoubleStrum()
{
	m_noteObj->setTexture(Note::texId_doublestrum);
	m_interrupted = true;
	if(m_bandeObj) m_bandeObj->setTexture(Note::texId_missed_band);
}

void Note::buildNoteObject()
{
	if(!m_noteObj)
	{
		Objet *o = new Objet();
		m_noteObj=o;

		Vec3 pos( float(m_track)-2.0f,-3.0f,-Note::SCALE*m_time);
		o->setPosition(pos);
		o->setScale(Vec3( 1.5f,1.1f,1.5f));

		o->setShader(note_shader);
		o->setGeometry(Note::note_geom);
		if(m_tostrum) o->setTexture(texId_standards[m_track]);
		else o->setTexture(texId_nostrums[m_track]);
	}
	else cout << "buildNoteObject : note object already built ! " << endl ;
}

void Note::buildBandeObject()
{
	if(!m_bandeObj)
	{
		Objet *o = new Objet();
		m_bandeObj=o;

		Vec3 pos( float(m_track)-2.0f,-3.0f,-Note::SCALE*m_time);
		o->setPosition(pos+Vec3( 0.0,0.0,-Note::SCALE*m_duration));
		o->setScale( Vec3( 1.5f,0.8f,Note::SCALE*m_duration));

		o->setGeometry(Note::bande_geom);
		o->setShader(note_shader);
		o->setTexture(texId_standard_bands[m_track]);
	}
	else cout << "buildBandeObject : band object already built ! " << endl ;
}

void Note::draw(float songtime)
{
	if(m_noteObj)
	{
		if ( m_noteObj->getScale().x < 1.5
			|| m_noteObj->getScale().y < 1.1
			|| m_noteObj->getScale().z < 1.5 )
			cout << "beuppp !" << endl ;

		if ( m_time<songtime+8.0 && m_time>songtime-0.8 )
			m_noteObj->draw();

		if( m_bandeObj )
		if( ( m_time+m_duration < songtime+8.0 && m_time+m_duration >songtime-0.8 )
			 || ( m_time<songtime+8.0 && m_time>songtime-0.8 )
			 || ( songtime >m_time && songtime<m_time+m_duration ) )
			m_bandeObj->draw();
	 }
}
